package dhy.lyb.change.service.impl;

import dhy.lyb.change.dao.UserDao;
import dhy.lyb.change.enums.MessageEnum;
import dhy.lyb.change.model.*;
import dhy.lyb.change.service.UserService;
import dhy.lyb.change.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2016/11/18.
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 获取具体dao类
     */
    @Autowired
    public void setBaseDao() {
        super.setBaseDao(userDao);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public int insertRecord(User user) {
        if (userDao.getRecordByCondition(user).size() != 0) {
            System.out.println(userDao.getRecordByCondition(user).size());
            User userRecord = userDao.getRecordByCondition(user).get(0);
            //判断账号
            if (userRecord.getMobile() != null || !("".equals(userRecord.getMobile()))) {
                return MessageEnum.TEL_EXITED.getValue();
            }
        }
        //数据加密
        String password = DataUtil.md5(user.getPassword());
        user.setPassword(password);
        //添加数据
        Integer count = userDao.insertRecord(user);
        if (count > 0) {

            return MessageEnum.OPERATE_SUCCESS.getValue();
        }
        return MessageEnum.OPERATE_ERROR.getValue();
    }

    public int checkMobile(User user){
        System.out.println(userDao.getRecordByCondition(user).size());
        if (userDao.getRecordByCondition(user).size() == 0) {
            return MessageEnum.OPERATE_ERROR.getValue();
        }else{
            return MessageEnum.TEL_EXITED.getValue();
        }
    }

    /**
     * 用户登录检测
     *
     * @param user
     * @return
     */
    public int checkLogin(User user) {
        String password = DataUtil.md5(user.getPassword());
        user.setPassword(password);
        //检查
        User users = userDao.getUserByPassword(user);
        if (users != null) {
            System.out.println(users);
            return users.getId();
        }
        return MessageEnum.OPERATE_ERROR.getValue();
    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    public int updatePasswordByMobile(User user) {
        //对password进行加密
        String password = DataUtil.md5(user.getPassword());
        user.setPassword(password);
        return userDao.updatePasswordByMobile(user);
    }

    /**
     * 获取用户列表
     * 如果还没有选择训练内容的用户 列表中将没有返回数据，因为从数据库中进行关联查询，则查不到没有任何关联的用户信息
     *
     * @return
     */
    public List<User> getUserActivityList() {
        return userDao.getUserActivityList();
    }

    /**
     * 获取单个用户的训练情况
     * 如果还没有选择训练内容的用户 activities字段直接返回null
     *
     * @param user
     * @return
     */
    public User getUserActivityById(User user) {
        User u = userDao.getRecordByCondition(user).get(0);
        //呵呵
        int count = userDao.checkUserActivity(user.getId());
        if (count != 0) {
            u = userDao.getUserActivityById(user);
        }
        return u;
    }

    /**
     * 获取一个用户的锻炼信息总量
     *
     * @param user
     * @return
     */
    public UserActivityCustom getUserActivityItemById(User user) {
        UserActivityCustom userActivityCustom = new UserActivityCustom();
        List<Activity> activityList = null;
        double uses_energy = 0;
        Integer uses_times = 0;
        Integer uses_day = 0;
        Integer uses_minute = 0;

        //信息初始化
        userActivityCustom.setId(user.getId());
        userActivityCustom.setUses_day(uses_day);
        userActivityCustom.setUses_energy(uses_energy);
        userActivityCustom.setUses_minute(uses_minute);
        userActivityCustom.setUses_times(uses_times);

        //从数据库获取列表
        int count = userDao.checkUserActivity(user.getId());
        if (count != 0) {
            User u = userDao.getUserActivityById(user);
            activityList = u.getActivities();
            for (int i = 0; i < activityList.size(); i++) {
                uses_energy = uses_energy + activityList.get(i).getUserActivityCustom().getUses_energy();
                uses_times = uses_times + activityList.get(i).getUserActivityCustom().getUses_day();
                uses_day = uses_day + activityList.get(i).getUserActivityCustom().getUses_times();
                uses_minute = uses_minute + activityList.get(i).getUserActivityCustom().getUses_minute();
            }
            userActivityCustom.setId(user.getId());
            userActivityCustom.setUses_day(uses_day);
            userActivityCustom.setUses_energy(uses_energy);
            userActivityCustom.setUses_minute(uses_minute);
            userActivityCustom.setUses_times(uses_times);
        }
        return userActivityCustom;
    }

    /**
     * 添加用户与训练内容的关联
     *
     * @param user_id
     * @param activity_id
     * @return
     */
    public int insertUserActivity(Integer user_id, Integer activity_id) {
        User user = new User();
        Activity activity = new Activity();
        user.setId(user_id);
        activity.setId(activity_id);
        int count = userDao.insertUserActivity(user, activity);
        if (count > 0) {
            return MessageEnum.OPERATE_SUCCESS.getValue();
        }
        return MessageEnum.OPERATE_ERROR.getValue();
    }

    /**
     * 删除用户与训练内容的关联
     *
     * @param user_id
     * @param activity_id
     * @return
     */
    public int deleteUserActivity(Integer user_id, Integer activity_id) {
        User user = new User();
        Activity activity = new Activity();
        user.setId(user_id);
        activity.setId(activity_id);
        int count = userDao.deleteUserActivity(user, activity);
        if (count > 0) {
            return MessageEnum.OPERATE_SUCCESS.getValue();
        }
        return MessageEnum.OPERATE_ERROR.getValue();
    }

    /**
     * 更新训练信息
     *
     * @param userActivity
     * @return
     */
    public int updateUserActivityUses(UserActivity userActivity) {
        return userDao.updateUserActivityUses(userActivity);
    }

    /**
     * 训练集合判断
     *
     * @param user_id
     * @return
     */
    public int checkUserActivity(Integer user_id) {
        return userDao.checkUserActivity(user_id);
    }

    /**
     * 添加用户与食物的关联
     *
     * @param user_id
     * @param food_id
     * @return
     */
    public int insertUserFood(Integer user_id, Integer food_id) {
        User user = new User();
        Food food = new Food();
        user.setId(user_id);
        food.setId(food_id);
        int count = userDao.insertUserFood(user, food);
        if (count > 0) {
            return MessageEnum.OPERATE_SUCCESS.getValue();
        }
        return MessageEnum.OPERATE_ERROR.getValue();
    }

    /**
     * 删除用户与食物的关联
     *
     * @param user_id
     * @param food_id
     * @return
     */
    public int deleteUserFood(Integer user_id, Integer food_id) {
        User user = new User();
        Food food = new Food();
        user.setId(user_id);
        food.setId(food_id);
        int count = userDao.deleteUserFood(user, food);
        if (count > 0) {
            return MessageEnum.OPERATE_SUCCESS.getValue();
        }
        return MessageEnum.OPERATE_ERROR.getValue();
    }
}
